import { reqGoodsInfo, reqAddOrUpdateShopCart } from '@/api'
import { getUUID } from '@/utils/uuid_token'
const state = {
    goodInfo: {},
    // 游客的临时身份
    uuid_token: getUUID()
}
const actions = {
    // 获取产品信息的actions
    async getGoodsInfo ({ commit }, skuId) {
        let res = await reqGoodsInfo(skuId)
        if (res.code == 200) {
            commit('GETGOODSINFO', res.data)
        }
    },
    //将产品添加到购物车中
    async addShopCartOrUpdate ({ commit }, { skuId, skuNum }) {
        let res = await reqAddOrUpdateShopCart(skuId, skuNum)
        if (res.code == 200) {
            return 'ok'
        } else {
            return Promise.reject(new Error('faile'))
        }
    }
}
const mutations = {
    GETGOODSINFO (state, goodInfo) {
        state.goodInfo = goodInfo
    }
}
const getters = {
    // 路径导航简化的数据
    categoryView (state) {
        return state.goodInfo.categoryView || {}
    },
    // 简化产品信息的数据
    skuInfo (state) {
        return state.goodInfo.skuInfo || {}
    },
    // 产品售卖简化
    spuSaleAttrList (state) {
        return state.goodInfo.spuSaleAttrList || []
    }
}

export default {
    state, actions, mutations, getters
}